package day08_stream_file_charset.com.itheima.d1_stream;

import java.util.*;
import java.util.stream.Collectors;

public class Test4_Stream {
    public static void main(String[] args) {
        ArrayList<Movie> movies = new ArrayList<>();
        movies.add(new Movie("《唐顿庄园》", 9.5, "张艺谋"));
        movies.add(new Movie("摔跤吧，爸爸", 9.5, "阿米尔汗"));
        movies.add(new Movie("三傻宝莱坞", 8.5, "阿米尔汗2"));
        movies.add(new Movie("三傻宝莱坞", 8.5, "阿米尔汗2"));
        movies.add(new Movie("阿甘正传", 7.5, "汤姆汉克斯"));
        movies.stream().forEach(System.out::println);

        System.out.println("------------------");

        long count = movies.stream().skip(2).count();
        System.out.println("count = " + count);
        System.out.println("------------------");

        Movie movie = movies.stream().max((o1, o2) -> Double.compare(o2.getScore(), o1.getScore())).get();
        System.out.println("movie = " + movie);
        System.out.println("------------------");
        ArrayList<String> list = new ArrayList<>();
        list.add("张三丰");
        list.add("张无忌");
        list.add("张三");
        list.add("张三1");

        List<String> names = list.stream().filter(s -> s.startsWith("张")).collect(Collectors.toList());
        System.out.println("names = " + names);

        System.out.println("------------------");
        names.add("张三2");
        System.out.println("names = " + names);
        System.out.println("-----------------------");
        Set<String> names2 = list.stream().filter(s -> s.startsWith("张")).collect(Collectors.toSet());
        names2.add("张三3");
        System.out.println("names2 = " + names2);

        System.out.println("-----------------------");
        Object[] names3 = list.stream().filter(s -> s.length() == 3).toArray();
        System.out.println("names3 = " + Arrays.toString(names3));

        System.out.println("-----------------------");
        String[] names4 = list.stream().filter(s -> s.length() == 3).toArray(String[]::new);
        System.out.println("names4 = " + Arrays.toString(names4));

        System.out.println("-----------------------");
        ArrayList<Movie> movies1 = new ArrayList<>();
        movies1.add(new Movie("唐顿庄园", 9.5, "张三丰"));
        movies1.add(new Movie("三傻宝莱坞", 8.5, "阿米尔汗"));
        movies1.add(new Movie("算加白",9.5,"阿米尔汗"));
        movies1.add(new Movie("摔跤吧，爸爸", 9.5, "阿米尔汗"));
        Map<String, Double> map = movies1.stream().distinct().collect(Collectors.toMap(m -> m.getName(), m -> m.getScore()));
        System.out.println("map = " + map);
    }
}
